{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.对象副本与视图\n",
    "\n",
    "- 对象的副本:与对象指向的内存不同；例子:ndarray.copy()操作\n",
    "- 对象的视图:与对象指向同一个内存；例如赋值操作、切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  [0 1 2 3 4 5 6 7 8 9]\n",
      "b:  [1 1 2 3 4 5 6 7 8 9]\n",
      "id(a)  2919296381616\n",
      "id(b)  2919296381056\n",
      "\n",
      "id(a):  2919296381616\n",
      "id(c):  2919296381616\n",
      "id(d):  2919296381376\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "\n",
    "# 副本\n",
    "b = a.copy()\n",
    "b[0] = 1\n",
    "print(\"a: \", a)\n",
    "print(\"b: \", b)\n",
    "print(\"id(a) \", id(a)) # 可以看到两者的地址是不一样的\n",
    "print(\"id(b) \", id(b))\n",
    "\n",
    "# 视图操作\n",
    "c = a\n",
    "d = a[1:10:2]\n",
    "print(\"\\nid(a): \", id(a)) # 可以看到三者的地址是一样的\n",
    "print(\"id(c): \", id(c))\n",
    "print(\"id(d): \", id(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.向量化\n",
    "\n",
    "矩阵之间的运算可以通过类似数学运算的形式进行，例如:矩阵之间的元素级运算 $A*B$,$A+B$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.广播机制\n",
    "\n",
    "广播机制：实现不同shape矩阵之间的运算或者函数处理。\n",
    "\n",
    "广播机制的兼容性条件有以下几条(满足其中之一即可使用广播机制)\n",
    "1. 两个矩阵之间的各个维度等长\n",
    "2. 其中一个矩阵的维度为1(缺失的数据使用维度1中的数据填充)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: \n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "b: \n",
      " [ 1.  1.  1.  1.]\n",
      "c: \n",
      " [[  1.   2.   3.   4.]\n",
      " [  5.   6.   7.   8.]\n",
      " [  9.  10.  11.  12.]\n",
      " [ 13.  14.  15.  16.]]\n",
      "1.02 µs ± 8.64 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n",
      "\n",
      "f.shape:  (2, 3, 5)\n",
      "d: \n",
      " [1 3 5 7 9]\n",
      "e: \n",
      " [[[0]\n",
      "  [1]\n",
      "  [2]]\n",
      "\n",
      " [[3]\n",
      "  [4]\n",
      "  [5]]]\n",
      "f: \n",
      " [[[ 1  3  5  7  9]\n",
      "  [ 2  4  6  8 10]\n",
      "  [ 3  5  7  9 11]]\n",
      "\n",
      " [[ 4  6  8 10 12]\n",
      "  [ 5  7  9 11 13]\n",
      "  [ 6  8 10 12 14]]]\n",
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(16).reshape(4, 4)\n",
    "b = np.ones((4))\n",
    "print(\"a: \\n\", a)\n",
    "print(\"b: \\n\", b)\n",
    "\n",
    "# b会使用广播机制扩充到[4,4]\n",
    "c = a + b\n",
    "print(\"c: \\n\", c)\n",
    "\n",
    "%timeit d = np.arange(6).reshape(2, 1, 3)\n",
    "e = np.arange(6).reshape(2, 3, 1)\n",
    "f = d + e # 存在维度1，d\\e都会进行扩维\n",
    "print(\"\\nf.shape: \", f.shape)\n",
    "print(\"d: \\n\", d)\n",
    "print(\"e: \\n\", e)\n",
    "print(\"f: \\n\", f)\n",
    "%time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
